/*!
 * Remark Material (http://getbootstrapadmin.com/remark)
 * Copyright 2017 amazingsurge
 * Licensed under the Themeforest Standard Licenses
 */

!function(global,factory){if("function"==typeof define&&define.amd)define("/State",["exports","jquery"],factory);else if("undefined"!=typeof exports)factory(exports,require("jquery"));else{var mod={exports:{}};factory(mod.exports,global.jQuery),global.State=mod.exports}}(this,function(exports,_jquery){"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _jquery2=babelHelpers.interopRequireDefault(_jquery),_class=function(){function _class(states){babelHelpers.classCallCheck(this,_class),this._states=Object.assign({},states),this._values={},this._relations={},this._callbacks={},this._define()}return babelHelpers.createClass(_class,[{key:"_define",value:function(){for(var _this=this,self=this,keys=Object.keys(this._states),obj={},tmpRelations=[],composites=[],i=0,l=keys.length;i<l;i++)!function(i,l){var key=keys[i];"function"!=typeof _this._states[key]?(Object.defineProperty(obj,key,{set:function(){return!1},get:function(){return tmpRelations.push(key),self._states[key]},enumerable:!0,configurable:!0}),_this._values[key]=_this._states[key],_this._relations[key]=[]):composites.push(key)}(i);for(var i=0,l=composites.length;i<l;i++)!function(i,l){var key=composites[i];Object.defineProperty(obj,key,{set:function(){return!1},get:function(){var value=self._states[key].call(obj);return self._addRelation(key,tmpRelations),tmpRelations=[],self._values[key]=value,value},enumerable:!0,configurable:!0})}(i)}},{key:"_compare",value:function(state){if(this._states[state]!==this._values[state]){var value=this._values[state];this._values[state]=this._states[state],this._dispatch(state,value,this._states[state]),this._compareComposite(state)}}},{key:"_compareComposite",value:function(state){var relations=this.getRelation(state);if(relations&&relations.length>0)for(var i=0,l=relations.length;i<l;i++){var _state=relations[i],value=this._states[_state]();value!==this._values[_state]&&(this._dispatch(_state,this._values[_state],value),this._values[_state]=value)}}},{key:"_addRelation",value:function(state,relations){for(var i=0,l=relations.length;i<l;i++){var pros=relations[i];this._relations[pros].push(state)}}},{key:"_dispatch",value:function(state,origValue,newValue){this._callbacks[state]&&this._callbacks[state].fire([newValue,origValue])}},{key:"getRelation",value:function(state){return this._relations[state].length>0?this._relations[state]:null}},{key:"on",value:function(state,callback){"function"==typeof state&&(callback=state,state="all"),this._callbacks[state]||(this._callbacks[state]=_jquery2.default.Callbacks()),this._callbacks[state].add(callback)}},{key:"off",value:function(state,callback){this._callbacks[state]&&this._callbacks[state].remove(callback)}},{key:"set",value:function(state,value){var isDeep=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if("string"==typeof state&&void 0!==value&&"function"!=typeof this._states[state])this._states[state]=value,isDeep||this._compare(state);else if("object"===(void 0===state?"undefined":babelHelpers.typeof(state))){for(var _key in state)"function"!=typeof state[_key]&&this.set(_key,state[_key],!0);for(var _key2 in state)"function"!=typeof state[_key2]&&this._compare(_key2)}return this._states[state]}},{key:"get",value:function(state){return state?this._values[state]:this._values}}]),_class}();exports.default=_class});